// asyncPackageComponent/components/orderRechargePay/index.js
let yz_pay = require("../../../mycomponent/yz_pay/yz_pay");
let skinBehavior = require("../../../utils/themeSkin/skinBehavior");
let app = getApp();
Component({
  behaviors: [yz_pay, skinBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      value: false,
      type: Boolean
    },
    extInfo: {
      value: {},
      type: Object
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    rechargeInfo: {},
    balanceLang: '余额',
    money: "",

    lackNum: ''
  },

  observers: {
    'show': function (val) {
      if (val) this.getRechargeData();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 初始化参数
    async getRechargeData() {
      let response = await app._postNetWork({
        url: app.getNetAddresss('finance.balance.balance'),
      });
      let { result, msg, data } = response.data;

      if (!result) return app.tips(msg);
      let _setData = {
        rechargeInfo: data
      };
      _setData.lackNum = (this.data.extInfo.orderNum - _setData.rechargeInfo.credit2).toFixed(2);
      _setData.isShowRecharge = true;
      _setData.discountType = _setData.rechargeInfo.remark && _setData.rechargeInfo.remark.proportion_status == 0 ? '元' : '%'
      this.setData(_setData)
      try {
        let value = wx.getStorageSync('balance');
        value = value || "余额";
        if (value) {
          this.setData({
            balanceLang: value
          });
        }
      } catch (e) {
        console.log(e);
      }
    },
    // 确认充值
    confirm_post(e) {
      let type = e.currentTarget.dataset.val;
      if (this.data.rechargeInfo.need_sign) {
        wx.showModal({
          title: '温馨提示',
          confirmText: '去签署',
          content: `根据平台要求，您需完成《${this.data.rechargeInfo.sign_info.contract_name}》合同签署后再充值${this.data.balanceLang}！`,
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/packageH/sign_shopV2/index/index?scene=7',
              });
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        return;
      }


      if (app._isTextEmpty(this.data.money)) {
        app.tips('输入值不能小于0')
        return
      }
      if (Number(this.data.money) < Number(this.data.lackNum)) {
        app.tips("输入值不能小于" + this.data.lackNum);
        return;
      }

      this.setData({
        money: parseFloat(this.data.money),
        pay_type: type
      });

      if (this.data.money <= 0) {
        wx.showToast({
          icon: 'none',
          title: '金额不可低于0',
          duration: 1500
        });
        return;
      }

      let urlStr = app.getNetAddresss('finance.balance.recharge');

      urlStr += '&client_type=2';
      urlStr += '&app_type=wechat';
      let _json = {
        pay_type: this.data.pay_type,
        recharge_money: this.data.money,
        order_pay_id: this.data.extInfo.order_pay_id
      }

      this.data.order_pay_id = this.data.extInfo.order_pay_id;
      this.data.order_sn = this.data.extInfo.pay_sn;

      app._postNetWork({
        url: urlStr,
        data: _json,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.close();
            if (this.data.pay_type == '88') {
              wx.showModal({
                title: '',
                showCancel: false,
                content: '充值成功',
                success: function (res) {
                  wx.navigateBack({
                    delta: 1
                  });
                }
              })
              return;
            }
            if (app._isTextEmpty(res.data.ordersn) && this.data.pay_type != '140' && this.data.pay_type != 132) {
              wx.showToast({
                icon: 'none',
                title: '参数错误',
                duration: 1500
              });
              return;
            }
            this.setData({
              ordersn: res.data.ordersn
            });
            this.rechargePay({
              pay_type: type,
              data: res,
              ordersn: res.data.ordersn,
              type: 'balance',
              callback: () => {
                this.checkoutConsumePacket();
              }
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
            if (type == 102) {
              if (res.data.eplus_bind_mobile == 1) {
                // 绑定手机
                wx.navigateTo({
                  url: '/packageF/others/wisdom/wisdomBind/wisdomBind?option=phone&to=pay',
                })
                app.tips(resdata.msg)
              }
            }
          }
        },
        fail: (res) => {
          console.log(res);
        }
      });
    },
    selectActivityNum(e) {
      let num = e.currentTarget.dataset.num;
      this.setData({
        money: num
      })
    },
    toSignV2() {
      wx.navigateTo({
        url: '/packageH/sign_shopV2/index/index?scene=7',
      });
    },
    toSignDetail() {
      wx.navigateTo({
        url: `/packageH/signPage/signPage?id=${this.data.rechargeInfo.sign_info.contract_id}`,
      });
    },
    close(){
      this.setData({
        show: false
      });
    }
  }
})